<?php
namespace shopend\controllers;

use common\models\Shop;
use common\models\Active;
use common\models\Order;
use common\service\ExcelService;
use common\service\GlobalUrlService;
use common\service\MenuService;
use common\service\ConstantService;

class IndexController extends BaseController
{
    public function actionIndex()
    {
        $this->layout = 'main';
        return $this->render('index');
    }

    public function actionGetMenu(){
        $menus = [
            ['title'=>'商品管理','icon'=>'&#xe653;','href'=>GlobalUrlService::buildShopUrl('goods/index'),'spread'=>false,'children'=>[
                ['title'=>'商品列表','icon'=>'&#xe663;','href'=>GlobalUrlService::buildShopUrl('goods/index'),'spread'=>false],
            ]],
            ['title'=>'活动管理','icon'=>'&#xe656;','href'=>GlobalUrlService::buildShopUrl('active/index'),'spread'=>false,'children'=>[
                ['title'=>'活动列表','icon'=>'&#xe663;','href'=>GlobalUrlService::buildShopUrl('active/index'),'spread'=>false],
            ]],
            ['title'=>'订单管理','icon'=>'&#xe65e;','href'=>GlobalUrlService::buildShopUrl('order/index'),'spread'=>false,'children'=>[
                ['title'=>'订单列表','icon'=>'&#xe663;','href'=>GlobalUrlService::buildShopUrl('order/index'),'spread'=>false],
            ]],
            ['title'=>'账户管理','icon'=>'&#xe659;','href'=>GlobalUrlService::buildShopUrl('withdraw/index'),'spread'=>false,'children'=>[
                ['title'=>'账户提现','icon'=>'&#xe663;','href'=>GlobalUrlService::buildShopUrl('withdraw/index'),'spread'=>false],
            ]],
            ['title'=>'投诉管理','icon'=>'&#x1006;','href'=>GlobalUrlService::buildShopUrl('complaint/index'),'spread'=>false,'children'=>[
                ['title'=>'投诉列表','icon'=>'&#xe663;','href'=>GlobalUrlService::buildShopUrl('complaint/index'),'spread'=>false],
            ]],
        ];
        $data['contentManagement'] = $menus;
        return $this->renderJson($data,'',ConstantService::$response_success);
    }
    /**
     * 后台首页
     * @return string
     */
    public function actionDashboard(){
        $start_time = strtotime(date('Y-m-d ') . '00:00:00');
        $end_time = strtotime(date('Y-m-d ') . '23:59:59');
        //需要统计的数据
        //1 日,总订单数
        $order_query = Order::find();
        $max_time = ['>=', 'created_time', $start_time];
        $min_time = ['<=', 'created_time', $end_time];
        $pay_status = ['=', 'pay_status', 1];
        $shop_ids = ['=', 'shop_id', $this->shop['id']];
        $noeq = ['!=', 'order_status', 6];
        $day_order = $order_query->select('count(id) as day_order_count')
            ->andWhere($max_time)
            ->andWhere($min_time)
            ->andWhere($pay_status)
            ->andWhere($shop_ids)
            ->andWhere(['>=', 'order_status', 1])
            ->asArray()
            ->one();
        $count_order = Order::find()->select('count(id) as order_count')
            ->andWhere($pay_status)
            ->andWhere($shop_ids)
            ->andWhere(['>=', 'order_status', 1])
            ->asArray()
            ->one();

        //2. 日,总商品销量 算法: 已完成
        $count_goods = Order::find()->select('sum(goods_num) as goods_count')
            ->andWhere($pay_status)
            ->andWhere($shop_ids)
            ->andWhere($noeq)
            ->asArray()
            ->one();
        //3 日,总销售金额 算法:已完成
            //日销售金额
            $day_money_ywc = $order_query->select('sum(order_money) as day_money_count')
                ->andWhere($max_time)
                ->andWhere($min_time)
                ->andWhere($pay_status)
                ->andWhere($noeq)
                ->andWhere($shop_ids)
                ->asArray()
                ->one();
            //总销售金额
            $count_money_ywc = Order::find()->select('sum(order_money) as money_count')
                ->andWhere($pay_status)
                ->andWhere($noeq)
                ->andWhere($shop_ids)
                ->asArray()
                ->one();

        //4. 退款订单
        $order_status_4 = Order::find()->select('count(id) as count_order')
            ->where(['shop_id'=>$this->shop['id'],'order_status'=>4,'is_delete'=>0])->asArray()->one();
        //5. 待发货订单
        $order_status_1 = Order::find()->select('count(id) as count_order')
            ->where(['shop_id'=>$this->shop['id'],'order_status'=>1])->asArray()->one();

        //总活动数量
        $active_count = Active::find()->where(['shop_id'=>$this->shop['id'],'is_del'=>0])->asArray()->count();
        $data = [
            'day_order' => $day_order['day_order_count'],
            'count_order' => $count_order['order_count'],
            'count_goods' => $count_goods['goods_count'],
            'active_count' => $active_count,
            'day_money' => sprintf("%.2f",$day_money_ywc['day_money_count']),
            'count_money' =>sprintf("%.2f",$count_money_ywc['money_count']),
            'order_status_4' =>$order_status_4['count_order'],
            'order_status_1' =>$order_status_1['count_order'],
        ];

        return $this->render('dashboard', ['data' => $data]);
    }

    public function actionGetCount(){
        $day = $this->post('day');
        $beg = $this->post('beg');
        $end = $this->post('end');
        if(!empty($beg) && !empty($end)){
            $data = Order::find()
                ->select(['weeks'=>"FROM_UNIXTIME(pay_time,'%Y-%m-%d')",'count'=>'COUNT(id)','money'=>'sum(order_money)'])
                ->andWhere(['shop_id'=>$this->shop['id']])
                ->andWhere(['>=','pay_time',strtotime($beg. '00:00:00')])
                ->andWhere(['<=','pay_time',strtotime($end . '23:59:59')])
                ->andWhere(['pay_status'=>1])
                ->orderBy(['pay_time'=>SORT_DESC])
                ->groupBy('weeks')
                ->asArray()
                ->all();

        } else {
            $data = Order::find()
                ->select(['weeks'=>"FROM_UNIXTIME(pay_time,'%Y-%m-%d')",'count'=>'COUNT(id)','money'=>'sum(order_money)'])
                ->limit($day)
                ->andWhere(['shop_id'=>$this->shop['id']])
                ->andWhere(['pay_status'=>1])
                ->orderBy(['pay_time'=>SORT_DESC])
                ->groupBy('weeks')
                ->asArray()
                ->all();

        }
        foreach ($data as &$val){
            $val['money'] = round($val['money'],2);
        }
        $data = array_reverse($data);
        $result['day'] = array_column($data,'weeks');
        $result['count'] = array_column($data,'count');
        $result['money'] = array_column($data,'money');
        return $this->renderJson($result,'',ConstantService::$response_success);
    }
}